#include "vendingmachine.h"
#include <iostream>
using namespace std;


VendingMachine::VendingMachine( Printer &prt, NameServer &nameServer, unsigned int id, unsigned int sodaCost, unsigned int maxStockPerFlavour ) {

}

bool VendingMachine::getFull() {
  return full;
}

VendingMachine::Status VendingMachine::buy( Flavours flavour, WATCard *&card ) {
	
	if(stockList[flavour] == 0) {
		return STOCK;
	} else if(card->getFund() < MSRP) {
		return FUNDS;
	} else {
     stockList[flavour] -= 1;
     card->subFund(MSRP);
     prt->print(Printer::Vending, id, 'B', flavour, stockList[flavour]);
     full = false;
     return BUY; 
   }
	
}
VendingMachine::Status VendingMachineCardEater::buy(Flavours flavour, WATCard *&card){
  unsigned int bal = card->getFund();
  if (prng(9) == 0) {
    delete card;
    card = NULL;
    if (stockList[flavour] == 0) {
      return STOCK;
    }
    if (bal < MSRP) {
      return FUNDS;
    }
    stockList[flavour] -= 1;
    full = false;
    prt->print(Printer::Vending, id, 'B', flavour, stockList[flavour]);
    return BUY;
  } else {
    if (stockList[flavour] == 0) {
      return STOCK;
    }
    if (bal < MSRP) {
      return FUNDS;
    }
    stockList[flavour] -= 1;
    full = false;
    card->subFund(MSRP);
    prt->print(Printer::Vending, id, 'B', flavour, stockList[flavour]);
    return BUY;
  }
}
VendingMachine::Status VendingMachineOverCharger::buy( Flavours flavour, WATCard *&card ) {
	
	if(stockList[flavour] == 0) {
		return STOCK;
	}
	if(card->getFund() < MSRP) {
		return FUNDS;
	}
	stockList[flavour] -= 1;
	card->subFund(MSRP);
   full = false;
   prt->print(Printer::Vending, id, 'B', flavour, stockList[flavour]);
	return BUY;		
	
}

unsigned int *VendingMachine::inventory() {
prt->print(Printer::Vending, id, 'r');
	return stockList;
}
unsigned int *VendingMachineCardEater::inventory() {
prt->print(Printer::Vending, id, 'r');
	return stockList;
}
unsigned int *VendingMachineOverCharger::inventory() {
prt->print(Printer::Vending, id, 'r');
	return stockList;
}

void VendingMachine::restocked() {
prt->print(Printer::Vending, id, 'R');
	full = true;
}
void VendingMachineCardEater::restocked() {
prt->print(Printer::Vending, id, 'R');
	full = true;
}
void VendingMachineOverCharger::restocked() {
prt->print(Printer::Vending, id, 'R');
	full = true;
}

unsigned int VendingMachine::cost() {
	return MSRP;
}
unsigned int VendingMachineCardEater::cost() {
	return MSRP;
}
unsigned int VendingMachineOverCharger::cost() {
	return MSRP;
}

unsigned int VendingMachine::getId() {
	return id;
}
unsigned int VendingMachineCardEater::getId() {
	return id;
}
unsigned int VendingMachineOverCharger::getId() {
	return id;
}

VendingMachine::~VendingMachine() {
  prt->print(Printer::Vending, id, 'F');
}
VendingMachineOverCharger::~VendingMachineOverCharger() {
}
VendingMachineCardEater::~VendingMachineCardEater() {
}

/*
class VendingMachineCardEater : public VendingMachine { // specific vending machine
	public:
		VendingMachineCardEater( Printer &prt, NameServer &nameServer, unsigned int id, unsigned int sodaCost, unsigned int maxStockPerFlavour );
		// member routines from VendingMachine
};

class VendingMachineOverCharger : public VendingMachine { // specific vending machine
	public:
		VendingMachineOverCharger( Printer &prt, NameServer &nameServer, unsigned int id, unsigned int sodaCost, unsigned int maxStockPerFlavour );
		// member routines from VendingMachine
};
*/

VendingMachineCardEater::VendingMachineCardEater( Printer &prt, NameServer &nameServer, unsigned int id, unsigned int sodaCost, unsigned int maxStockPerFlavour ) : VendingMachine(prt,nameServer,id,sodaCost,maxStockPerFlavour ) {

  this->id = id;
	this->MSRP = sodaCost;
   prt.print(Printer::Vending, id, 'S', MSRP);
	stockList[0] = 0;
	stockList[1] = 0;
	stockList[2] = 0;
	stockList[3] = 0;
	full = false;
  this->prt = &prt;
	nameServer.VMregister( this );
}

VendingMachineOverCharger::VendingMachineOverCharger( Printer &prt, NameServer &nameServer, unsigned int id, unsigned int sodaCost, unsigned int maxStockPerFlavour ) : VendingMachine(prt,nameServer,id,sodaCost,maxStockPerFlavour ) {
        this->id = id;
	this->MSRP = sodaCost * 2;
   prt.print(Printer::Vending, id, 'S', MSRP);
	stockList[0] = 0;
	stockList[1] = 0;
	stockList[2] = 0;
	stockList[3] = 0;
	full = false;
  this->prt = &prt;
	nameServer.VMregister( this );
}


